function ismobile() {
    //判断访问终端
    var browser = {
        versions: function () {
            var u = navigator.userAgent, app = navigator.appVersion;
            return {
                trident: u.indexOf('Trident') > -1, //IE内核
                presto: u.indexOf('Presto') > -1, //opera内核
                webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
                gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1,//火狐内核
                mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
                ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
                android: u.indexOf('Android') > -1 || u.indexOf('Adr') > -1, //android终端
                iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
                iPad: u.indexOf('iPad') > -1, //是否iPad
                webApp: u.indexOf('Safari') == -1, //是否web应该程序，没有头部与底部
                weixin: u.indexOf('MicroMessenger') > -1, //是否微信 （2015-01-22新增）
                qq: u.match(/\sQQ/i) == " qq" //是否QQ
            };
        }(),
        language: (navigator.browserLanguage || navigator.language).toLowerCase()
    };

    if (browser.versions.mobile == true) {
        if (browser.versions.iPhone == true) {
            return '1';
        } else if (browser.versions.android == true) {
            return '0';
        }
    } else {

    }
}

var isMobile = ismobile();

//$(function () {

//getId();

var token;//注释

var uid;

if (isMobile == 0) {

    window.android.getAndroidId("pp");
}

function WebViewDidFinishLoad() {

    var message = {

        "VcWay": "SendId"
    };

    window.webkit.messageHandlers.CallDiffusionModel.postMessage(message);
}
//});

//var token = "1C57D351-6D36-0357-EC7F-F6E8426302E5";//注释
//
//var uid = 30;
//
//getNav()


var belong_model_xuanfa = 500;

var xuanfaCaseBelong_model = 510;

var curNavIndex = 0;

var curNavIndex1 = 0;

var mescrollrr = [];

var mescrollrr1 = [];

var paramsArr = [];

var paramsArr1 = [];


function getId() {

    if (isMobile == 1) {

        CallDiffusionModel.jsCallOCWithSendId()

    } else if (isMobile == 0) {

        window.android.getAndroidId("pp");
    }
}


function receiveAndroidDetail(k) {

    obj = $.parseJSON(k);

    token = obj.TOKEN;

    uid = obj.uid;

    getNav();
}

function receiveiOSVcData(k) {

    obj = $.parseJSON(k);

    token = obj.TOKEN;

    uid = obj.uid;

    getNav()
}

function getNav() {

    $.ajax({

        type: "POST",

        url: url + "users/fourModel",

        dataType: "json",

        data: {

            "url": "fourModel/catList",

            "belong_model": xuanfaCaseBelong_model
        },
        beforeSend: function (xhr) {

            xhr.setRequestHeader("TOKEN", token)

        },
        success: function (data) {

            //console.log(data);

            if (data.codes == 200) {

                var html = template("headerTpl", data);

                $("#ul").append(html);

                var html1 = template("xuanfaListTpl", {data: data.data.list});

                $("#action").html(html1);

                curNavIndex = 0;

                paramsArr = new Array(data.data.list.length + 1);

                paramsArr[0] = initParams(0, uid);

                mescrollrr = new Array(data.data.list.length + 1);

                mescrollrr[0] = initMescroll("mescroll0", "dataList0", upCallback);


                navClick();
            }

            //$("a").on("touchstart",function(){
            //    $(this).css("text-decoration","none")
            //});
        },
        error: function (info) {

            console.log(info)
        }
    });
}

function initParams(cat_id, uid) {

    var params = new Params(1, 10, cat_id, uid, xuanfaCaseBelong_model);

    return params;
}

function Params(page, size, cat_id, uid, belong_model) {

    this.page = page;

    this.size = size;

    this.cate_id = cat_id;

    this.uid = uid;

    this.belong_model = belong_model;

}

function navClick() {

    $("#ul").on("click", "li>a", function () {

        $("#ul a").removeClass("active");

        $(this).addClass("active").css("text-decoration", "none");

        var index = Number($(this).data("index"));

        if (curNavIndex != index) {

            //隐藏当前列表和回到顶部按钮

            $("#mescroll" + curNavIndex).hide().addClass("hide");

            mescrollrr[curNavIndex].hideTopBtn();

            //显示对应的列表

            $("#mescroll" + index).show().removeClass("hide");

            //取出菜单所对应的mescroll对象,如果未初始化则初始化

            if (paramsArr[index] == null || mescrollrr[index] == null) {

                paramsArr[index] = initParams($(this).attr("id"), uid);

                mescrollrr[index] = initMescroll("mescroll" + index, "dataList" + index, upCallback);

            } else {

                //检查是否需要显示回到到顶按钮

                var curMescroll = mescrollrr[index];

                var curScrollTop = curMescroll.getScrollTop();

                if (curScrollTop >= curMescroll.optUp.toTop.offset) {

                    curMescroll.showTopBtn();

                } else {

                    curMescroll.hideTopBtn();
                }
            }
            //更新标记
            curNavIndex = index;
        }
    });
}

//上拉刷新，下拉加载
function initMescroll(mescrollId, clearEmptyId, upCallback) {

    var mescroll = new MeScroll(mescrollId, { //第一个参数"mescroll"对应上面布局结构div的id

        up: {

            callback: upCallback, //上拉加载的回调

            noMoreSize: 3,

            isBounce: false,

            clearEmptyId: clearEmptyId,

            empty: {

                icon: "../../person_home/img/wallet_img_no@3x.png", //图标,默认null

                tip: "暂无相关数据~  再逛逛~ " //提示
            }
        }
    });

    return mescroll;

}

//上拉加载的回调
function upCallback(page) {

    var dataIndex = curNavIndex; //记录当前联网的nav下标,防止快速切换时,联网回来curNavIndex已经改变的情况;

    var params = paramsArr[dataIndex];

    params.page = page.num;

    params.size = page.size;

    //联网加载数据

    getListDataFromNet(dataIndex, params, function (data) {

        var listData = data.data;

        //必传参数(当前页的数据个数, 总数据量)

        if (data.codes == 200) {

            mescrollrr[dataIndex].endBySize(listData.list.length, listData.totalCount);

            setListData(data, dataIndex);

        } else {

            mescrollrr[dataIndex].endBySize(0, 0);

            mescrollrr[dataIndex].showEmpty();

            $("#case").hide()

        }

    }, function () {

        //联网失败的回调,隐藏下拉刷新和上拉加载的状态;

        mescrollrr[dataIndex].endErr();
    });
}

//从服务器获取数据
function getListDataFromNet(dataIndex, params, successCallback, errorCallback) {

    //alert(JSON.stringify(params))

    $.ajax({

        url: url + 'common',

        type: "POST",

        dataType: "json",

        data: params,

        beforeSend: function (xhr) {

            xhr.setRequestHeader("CUSTOM-URL", "v1/fourModel/makeTeamListWeb");

            xhr.setRequestHeader("TOKEN", token)

        },

        success: function (data) {

            //console.log(data)

            successCallback(data);

        },
        error: errorCallback
    });
}

function setListData(data, dataIndex) {

    //console.log(data)

    var dataList = "dataList" + dataIndex;

    var html = template("listTpl", data);

    $("#" + dataList).append(html);

    if (isiphoneX) {

        $(".footer_position").addClass("footer");

        $(".mescroll").css("padding-bottom", "0.484rem")

    } else {

        $(".footer_position").removeClass("footer")
    }

    data.data.list.forEach(function (item, i) {

        if (item.like_info == 100) {//没有点赞


        } else if (item.like_info == 200) {//点赞

            $("#" + dataList + " .b" + i).addClass("yes")

        }

    });

    var isClick = true;

    $(".work").on("click", "b", function (e) {

        e.stopPropagation();

        if (isClick) {

            if ($(this).data("info") == 100) {

                like(100, uid, $(this).parents("li").data("id"), xuanfaCaseBelong_model, $(this));

            } else {

                like(200, uid, $(this).parents("li").data("id"), xuanfaCaseBelong_model, $(this));

            }

            isClick = !isClick;

        }
        setTimeout(function () {

            isClick = true;

        }, 1000)


    });


}

$("#top").on("touchstart", ".top_left>a", function (e) {

    e.preventDefault();

    $(".top_right>a").removeClass("active");

    $(this).addClass("active");

    $("#action").show();

    $("header #ul").show();

    $("header #caseUl").hide();

    $("#case").hide();

    $(this).css("text-decoration", "none")

});

$(".top_right").on("touchstart", "a", function (e) {
    e.preventDefault();

    $(".top_left>a").removeClass("active");

    $(this).addClass("active");

    $("#case").show();

    $("header #ul").hide();

    $("header #caseUl").show();

    $("#action").hide();

    $(this).css("text-decoration", "none");


    if (!paramsArr1[0]) {

        getNav1()
    }

});

//案例展示

function getNav1() {
    $.ajax({

        type: "POST",

        url: url + "users/fourModel",

        dataType: "json",

        data: {
            "url": "fourModel/catList",

            "belong_model": belong_model_xuanfa
        },

        beforeSend: function (xhr) {

            xhr.setRequestHeader("TOKEN", token)

        },

        success: function (data) {

            //console.log(data);

            if (data.codes == 200) {

                var html = template("caseHeaderTpl", data);

                $("#caseUl").append(html);

                var html1 = template("caseTpl", {data: data.data.list});

                $("#case").html(html1);

                curNavIndex1 = 0;

                paramsArr1 = new Array(data.data.list.length + 1);

                paramsArr1[0] = initParamss(0, uid);

                mescrollrr1 = new Array(data.data.list.length + 1);

                mescrollrr1[0] = initMescroll1("scroll0", "List0", downCallback);

                //console.log(curNavIndex, paramsArr, mescrollrr);


                $("#caseUl").on("click", "li>a", function (e) {

                    e.preventDefault();

                    $("#caseUl>li>a").removeClass("active");

                    $(this).addClass("active").css("text-decoration", "none");

                    var index = Number($(this).data("index"));

                    //console.log(curNavIndex, index, mescrollId, dataListId);

                    if (curNavIndex1 != index) {

                        //隐藏当前列表和回到顶部按钮

                        $("#scroll" + curNavIndex1).hide().addClass("hide");

                        mescrollrr1[curNavIndex1].hideTopBtn();

                        //显示对应的列表

                        $("#scroll" + index).show().removeClass("hide");

                        //取出菜单所对应的mescroll对象,如果未初始化则初始化

                        if (paramsArr1[index] == null || mescrollrr1[index] == null) {

                            paramsArr1[index] = initParamss($(this).attr("id"), uid);

                            mescrollrr1[index] = initMescroll1("scroll" + index, "List" + index, downCallback);

                        } else {//检查是否需要显示回到到顶按钮

                            var curMescroll = mescrollrr1[index];

                            var curScrollTop = curMescroll.getScrollTop();

                            if (curScrollTop >= curMescroll.optUp.toTop.offset) {

                                curMescroll.showTopBtn();

                            } else {

                                curMescroll.hideTopBtn();
                            }
                        }

                        //更新标记

                        curNavIndex1 = index;
                    }


                });
            }

        },
        error: function (info) {

            console.log(info)
        }
    });
}

function initMescroll1(mescrollId, clearEmptyId, downCallback) {

    var mescroll1 = new MeScroll(mescrollId, {

        up: {

            callback: downCallback, //上拉回调,此处可简写; 相当于 callback: function (page) { getListData(page); }

            clearEmptyId: clearEmptyId, //1.下拉刷新时会自动先清空此列表,再加入数据; 2.无任何数据时会在此列表自动提示空

            isBounce: false, //此处禁止ios回弹,解析(务必认真阅读,特别是最后一点): https://www.mescroll.com/qa.html#q10

            noMoreSize: 3, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看

            empty: {

                // warpId :"dataList",

                icon: "../../person_home/img/wallet_img_no@3x.png", //图标,默认null

                tip: "暂无相关数据~  再逛逛~ "
            }
        }
    });

    return mescroll1;
}

function Paramss(page, size, uid, cat_id, belong_model) {

    this.page = page;

    this.size = size;

    this.uid = uid;

    this.type_id = cat_id;

    this.belong_model = belong_model;

}

function initParamss(cat_id, uid) {

    var params = new Paramss(1, 10, uid, cat_id, belong_model_xuanfa);

    return params;
}

function downCallback(page) {

    var dataIndex = curNavIndex1; //记录当前联网的nav下标,防止快速切换时,联网回来curNavIndex已经改变的情况;

    var params = paramsArr1[dataIndex];

    params.page = page.num;

    params.size = page.size;


    teamDetails(params, function (data) {

        //联网成功的回调,隐藏下拉刷新和上拉加载的状态;

        if (data.codes == 200) {

            var length = data.data.list.length;

            var count = data.data.maxPage;

            mescrollrr1[dataIndex].endByPage(length, count); //必传参数(当前页的数据个数, 总页数)

            setListData1(data, dataIndex);

        } else {

            mescrollrr1[dataIndex].endByPage(0, 0); //必传参数(当前页的数据个数, 总页数)

            mescrollrr1[dataIndex].showEmpty();

            $("#action").hide()

        }
    }, function () {

        //联网失败的回调,隐藏下拉刷新和上拉加载的状态;

        mescrollrr1[dataIndex].endErr();
    });
}

function teamDetails(params, successCallback, errorCallback) {

    //alert(JSON.stringify(params))

    $.ajax({

        type: "POST",

        url: url + 'common',

        dataType: "json",

        data: params,

        beforeSend: function (xhr) {

            xhr.setRequestHeader("CUSTOM-URL", "v1/dispersion/caseList");

            xhr.setRequestHeader("TOKEN", token)

        },

        success: function (data) {

            //console.log(data);

            successCallback(data)

        },

        error: errorCallback
    });
}

function setListData1(data, dataIndex) {

    var dataList = "List" + dataIndex;

    var html = template("teamTpl", data);

    $("#" + dataList).append(html);

    if (isiphoneX) {

        $(".mescroll").css("padding-bottom", "0.484rem")
    }

    data.data.list.forEach(function (item, i) {

        if (item.like_info == 100) {//没有点赞


        } else if (item.like_info == 200) {//点赞

            $("#" + dataList + " .b" + i).addClass("yes")

        }

    });

    var isClick = true;

    $(".thumbs").on("click", "b", function (e) {

        e.stopPropagation();

        if (isClick) {

            if ($(this).data("info") == 100) {

                like(100, uid, $(this).parents("li").attr("id"), belong_model_xuanfa, $(this));

            } else {

                like(200, uid, $(this).parents("li").attr("id"), belong_model_xuanfa, $(this));

            }

            isClick = !isClick;

        }
        setTimeout(function () {

            isClick = true;

        }, 1000)


    });

}

function like(status, uid, target_id, belong_model_area, that) {

    $.ajax({

        url: url + 'common',

        type: "POST",

        dataType: "json",

        data: {
            "status": status,

            "uid": uid,

            "like_model": belong_model_area,

            "target_id": target_id
        },

        beforeSend: function (xhr) {

            xhr.setRequestHeader("CUSTOM-URL", "v1/fourModel/like");

            xhr.setRequestHeader("TOKEN", token)

        },
        success: function (result) {

            //console.log(result)

            if (result.codes == 200) {

                if (status == 200) {//取消

                    that.data("info", 100);

                    that.removeClass("yes");

                    that.siblings("i").html(Number(that.siblings("i").html()) - 1)

                } else if (status == 100) {//点赞

                    that.data("info", 200);

                    that.addClass("yes");

                    that.siblings("i").html(Number(that.siblings("i").html()) + 1)

                }
            }

        },
        error: function (info) {

            console.log(info)
        }

    })
}

//团队资源 点击每个条目进详情
var isTrue = true;

$("#action").on("click", "li", function () {

    if (isTrue) {

        var liId = $(this).data("id");

        if (!liId)return false;

        var json = {

            "title": '宣发团队详情',

            "url": 'https://viewapi.aipics.cn/view_h5/four_plates/xuanfa/teamDetail.html',

            "team_id": String(liId),

            "TOKEN": token,

            "belong_model": xuanfaCaseBelong_model
        };

        if (isMobile == 1) {

            var message = {

                "VcData": json,

                "VcName": "DiffusionDetails",

                "VcWay": "PushVCName"
            };

            window.webkit.messageHandlers.CallDiffusionModel.postMessage(message);

        } else if (isMobile == 0) {

            window.android.jumpToProjectDisperingDetail(JSON.stringify(json));
        }

        isTrue = false;

    }
    setTimeout(function () {

        isTrue = true;

    }, 500);
});

//案例列表 点击每个条目详情
var isTrue1 = true;

$("#case").on("click", "li", function () {

    if (isTrue1) {

        var liId = $(this).attr("id");

        if(!liId) return false;

        var json = {
            "title": '宣发案例详情',

            "url": 'https://viewapi.aipics.cn/view_h5/four_plates/xuanfa/xuanfaDetail.html',

            "team_id": liId,

            "TOKEN": token
        };

        if (isMobile == 1) {

            var message = {

                "VcData": json,

                "VcName": "DiffusionDetails",

                "VcWay": "PushVCName"
            };

            window.webkit.messageHandlers.CallDiffusionModel.postMessage(message);

        } else if (isMobile == 0) {

            window.android.jumpToProjectFourModelDetail(JSON.stringify(json));

        }

        isTrue1 = false;

    }
    setTimeout(function () {

        isTrue1 = true;

    }, 500);

});


